package com.atguigu.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.entity.AttrEntity;
import com.atguigu.product.service.AttrAttrgroupRelationService;
import com.atguigu.product.service.AttrService;
import com.atguigu.product.service.CategoryService;
import com.atguigu.product.vo.AttrGroupRelationVo;
import com.atguigu.product.vo.AttrGroupWithAttrVo;
import com.atguigu.product.entity.AttrGroupEntity;
import com.atguigu.product.service.AttrGroupService;
import org.springframework.web.bind.annotation.*;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author polarisink
 * @email polarisink@163.com
 * @date 2020-12-18 14:19:34
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
  @Resource
  private AttrGroupService attrGroupService;

  @Resource
  private CategoryService categoryService;

  @Resource
  private AttrService attrService;


  @Resource
  AttrAttrgroupRelationService relationService;

  @PostMapping("/attr/relation")
  public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {

    relationService.saveBatch(vos);
    return R.ok();
  }


  @GetMapping("/{attrgroupId}/attr/relation")
  public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
    List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
    return R.ok().put("data", entities);
  }

  @GetMapping("/{attrgroupId}/noattr/relation")
  public R attrNoRelation(@PathVariable String attrgroupId,
                          @RequestParam Map<String, Object> params) {
    PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
    return R.ok().put("page", page);
  }


  @PostMapping("/attr/relation/delete")
  public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
    attrService.deleteRelation(vos);
    return R.ok();
  }


  /**
   * 列表
   */
  @RequestMapping("/list/{catelogId}")
  public R list(@RequestParam Map<String, Object> params,
                @PathVariable Long catelogId) {
    //PageUtils page = attrGroupService.queryPage(params);
    PageUtils page = attrGroupService.queryPage(params, catelogId);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrGroupId}")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
    Long catelogId = attrGroup.getCatelogId();
    Long[] path = categoryService.findCatelogPath(catelogId);
    attrGroup.setCatelogPath(path);
    return R.ok().put("attrGroup", attrGroup);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.save(attrGroup);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.updateById(attrGroup);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] attrGroupIds) {
    attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

    return R.ok();
  }

  /**
   * 获取分类下所有分组&关联属性
   */
  @GetMapping("/{catelogId}/withattr")
  public R getAttrGroupWithAttrByCatelogId(@PathVariable("catelogId") Long catId) {
    List<AttrGroupWithAttrVo> groupWithAttrVos = attrGroupService.getAttrGroupWithAttrByCatelogId(catId);
    return R.ok().put("data", groupWithAttrVos);
  }

}
